package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.vo.attrGroupVo;
import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String catId = (String) params.get("catId");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (catId!=null){
            wrapper.eq("catelog_id",catId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getByGroupId(Long attrGroupId) {
        List<Long> list = new ArrayList<>();
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrGroupId);
        getAttrGroup(list,attrGroupEntity.getCatelogId());
        Collections.reverse(list);
        attrGroupEntity.setPaths(list.toArray(new Long[list.size()]));
        return attrGroupEntity;
    }

    @Override
    public List<attrGroupVo> withattr(Long catelogId) {
        //根据分类id查询每个属性分组的每个规格属性
        List<AttrGroupEntity> attrGroups = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<attrGroupVo> collect = attrGroups.stream().map(item -> {
            attrGroupVo attrGroupVo = new attrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            List<AttrEntity> attr_group_id = attrDao.selectList(
                    new QueryWrapper<AttrEntity>().eq("attr_group_id", item.getAttrGroupId())
            .and(w->w.eq("attr_type",1).or().eq("attr_type",2)));
            attrGroupVo.setAttrs(attr_group_id);
            return attrGroupVo;
        }).collect(Collectors.toList());
        return collect;
    }

    private void getAttrGroup(List<Long> list,Long catId){
        list.add(catId);
        CategoryEntity categoryEntity = categoryDao.selectById(catId);
        if (!categoryEntity.getParentCid().equals(0L)){
            getAttrGroup(list,categoryEntity.getParentCid());
        }
    }

}